package com.lcache.springboot.controller;

import com.lcache.common.api.ApiResponse;
import com.lcache.springboot.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: lcache
 * @description: 缓存控制器类
 * @author: Alex Wu
 * @createDate: 2025-04-22 16:24
 **/
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    @GetMapping(value = "/get/{key}")
    public ApiResponse<String> get(@PathVariable  String key) {
        Object v = cacheService.get(key);
        if (v == null) {
            return ApiResponse.fail(400, "key not exist");
        }
        return ApiResponse.success(v.toString());
    }

    @GetMapping(value = "/new/{key}/{value}")
    public ApiResponse<String> put(@PathVariable String key,@PathVariable Object value) {
        cacheService.put(key, value);
        return ApiResponse.success("");
    }
}
